package mn.slide.laborberlin.tabgroup;

import mn.slide.laborberlin.db.DataLayer;
import mn.slide.laborberlin.main.R;
import mn.slide.laborberlin.main.TabBarActivity;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.LinearLayout;

public class GroupStarter {

	static private GroupStarter instance;

	static public void init() {
		if (null == instance) {
			instance = new GroupStarter();
		}
	}

	static public GroupStarter getInstance() {
		return instance;
	}

	public void startActivity(Activity context, Intent intent) {
		int groupIndex = ((TabBarActivity) context.getParent().getParent())
				.getTabIndex();
		switch (groupIndex) {
		case 0:
			HomeGroupActivity.group.goTo(intent);
			break;
		case 1:
			FavoriteGroupActivity.group.goTo(intent);
			break;
		case 2:
			HistoryGroupActivity.group.goTo(intent);
			break;

		default:
			break;
		}

	}

	public void onBackpressed(Activity context) {
		int groupIndex = ((TabBarActivity) context.getParent().getParent())
				.getTabIndex();
		final LinearLayout add_bg_lout = (LinearLayout) context.getParent()
				.getParent().findViewById(R.id.add_bg_lout);

		if (add_bg_lout.getVisibility() == View.VISIBLE) {
			LinearLayout addBtnBg = (LinearLayout) context.getParent()
					.getParent().findViewById(R.id.add_btn_bg);
			Animation outToBottm = AnimationUtils.loadAnimation(context,
					R.anim.out_to_bottom);
			addBtnBg.setAnimation(outToBottm);
			addBtnBg.setVisibility(View.GONE);
			outToBottm.setAnimationListener(new AnimationListener() {

				@Override
				public void onAnimationStart(Animation animation) {
					// TODO Auto-generated method stub

				}

				@Override
				public void onAnimationRepeat(Animation animation) {
					// TODO Auto-generated method stub

				}

				@Override
				public void onAnimationEnd(Animation animation) {
					add_bg_lout.setVisibility(View.GONE);
				}
			});

		} else {
			switch (groupIndex) {
			case 0:
				HomeGroupActivity.group.back();
				break;
			case 1:
				FavoriteGroupActivity.group.back();
				break;
			case 2:
				HistoryGroupActivity.group.back();
				break;

			default:
				break;
			}
		}
	}

	public void onBackShowDetail(Activity context) {
		int groupIndex = ((TabBarActivity) context.getParent().getParent())
				.getTabIndex();
		switch (groupIndex) {
		case 0:
			DataLayer.layer.detialHome = 1;
			break;
		case 1:
			DataLayer.layer.detialMerk = 1;
			break;
		case 2:
			DataLayer.layer.detialVerl = 1;
			break;

		default:
			break;
		}
	}
}
